
/*
 *  Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.core.modal.report;

import com.google.common.collect.Lists;
import com.intelligent.ispc.common.utils.StringUtils;

import javax.swing.table.AbstractTableModel;
import java.util.List;

/**
 * Created by Lucien.Chen on 2016/8/29.
 */
public class YieldHistoryDataModel extends AbstractTableModel {
    private String[] columns;
    private List<String[]> data;

    /**
     * construction
     *
     * @param columns all columns name
     */
    public YieldHistoryDataModel(String[] columns) {
        this.columns = columns;
    }

    @Override
    public int getRowCount() {
        if (data == null) {
            return 0;
        } else {
            return data.size();
        }
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        String[] columnValues = data.get(rowIndex);
        if (columnValues != null && columnValues.length > 0 && columnIndex < columns.length) {
            String value = columnValues[columnIndex];
            if (value == null) {
                value = "";
            }
            return value;
        }
        return null;
    }

    /**
     * @param data new table data
     */
    public void setData(List<String[]> data) {
        if (data == null) {
            data = Lists.newArrayList();
        }
        this.data = data;
        fireTableDataChanged();
    }

    @Override
    public Class<?> getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    private String isBlank(String value) {
        if (!StringUtils.isBlank(value)) {
            return value;
        } else {
            return "-";
        }
    }

}
